<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     Pagizer
 * @subpackage  backoffice
 */

class m_backoffice_actions_block_DeleteGenericAction extends m_backoffice_extends_FormModal
{
	public function execute()
	{
        $docId = $this->getRequestParameter("docId");
        $docLang = $this->getRequestParameter("docLang");
        $recursive = $this->getRequestParameter("recursive", false);
        $nyroModal = $this->getRequestParameter("nyroModal");
        $refresh = $this->getRequestParameter("refresh", 0);
        
        $errors = array();
        $link = "currentPage";
       
        if(!is_null($docId))
        {
            $doc = $this->getDocumentProvider()->getByUniqueId($docId, $docLang);
        }

        $aliases = m_backoffice_services_AliasService::getAliasFromDocument($doc);

        if(count($aliases) > 0)
        {
            foreach($aliases as $alias)
            {
                $alias->delete();
            }
        }

		if($nyroModal == 1 && isset($doc))
		{
            if(isset($doc) && $doc instanceof f_document_baseDocument)
            {
                $parent = $doc->getDocumentParent();

                ((bool)$recursive && $recursive == "true") ? $doc->deleteRecursive() : $doc->delete();

                if(!is_null($parent) && $parent instanceof f_document_baseDocument && $refresh == 0)
                {
                    if($parent->getModel() == 'modules_website/website')
                    {
                        f_core_Request::getInstance()->setParameter('configModule', $doc->getDocumentModuleName());
                    }

                    $link = $parent->getEditUrl();
                }
                
                $errors["status"] = "success";
            }
		}

        $this->addToModel("refresh", $refresh);
        $this->addToModel("errors", $errors);
        $this->addToModel("link", $link);
        $this->addToModel("recursive", $recursive);
		$this->addToModel('doc', $doc);
		
		$this->setView('success');
	}	
	
	public function validate()
	{        
		return $this->getUserContext()->hasRight("backoffice_accessBo");
	}
	
	public function errorHandler()
	{
		return null;
	}
}